unit uFiltro_Movimento;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Mask, ToolEdit, ExtCtrls, Grids, DBGrids,
  FMTBcd, DB, SqlExpr, Provider, DBClient, dxCore, dxButton, EDBFind,
  XPMenu, DBCtrls, rpcompobase, rpvclreport;

type
  TfFiltro_Movimento = class(TForm)
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    BitBtn6: TBitBtn;
    BitBtn8: TBitBtn;
    sds1: TSQLDataSet;
    DBGrid2: TDBGrid;
    sds1NOME_PROPRIETARIO: TStringField;
    sds1NOME_INQUILINO: TStringField;
    sds1VENCIMENTO: TDateField;
    sds1TOTAL_ALUGUEL: TFloatField;
    sds1TOTAL_RECEBIDO: TFloatField;
    sds1TOTAL_DESPESAS: TFloatField;
    sds1TOTAL_RECEITAS: TFloatField;
    DataSource1: TDataSource;
    cds1: TClientDataSet;
    dsp1: TDataSetProvider;
    cds1NOME_PROPRIETARIO: TStringField;
    cds1NOME_INQUILINO: TStringField;
    cds1VENCIMENTO: TDateField;
    cds1TOTAL_ALUGUEL: TFloatField;
    cds1TOTAL_RECEBIDO: TFloatField;
    cds1TOTAL_DESPESAS: TFloatField;
    cds1TOTAL_RECEITAS: TFloatField;
    sds1COD_H_IMALU: TIntegerField;
    cds1COD_H_IMALU: TIntegerField;
    sds2: TSQLDataSet;
    dsp2: TDataSetProvider;
    cds2: TClientDataSet;
    dt2: TDataSource;
    sds2COD_LANCAMENTO: TIntegerField;
    sds2COD_FIXOS: TIntegerField;
    sds2COD_IMALU: TIntegerField;
    sds2COD_CONTAS: TIntegerField;
    sds2QUTDE: TIntegerField;
    sds2VALOR: TFloatField;
    sds2VALOR_TOTAL: TFloatField;
    sds2DESCRICAO: TStringField;
    sds2PARCELA: TIntegerField;
    sds2VENCIMENTO: TDateField;
    sds2DATA_LANC: TDateField;
    sds2COD_INQUILINO: TIntegerField;
    sds2COD_MOVIMENTO: TIntegerField;
    sds2DESCRICAO_1: TStringField;
    cds2COD_LANCAMENTO: TIntegerField;
    cds2COD_FIXOS: TIntegerField;
    cds2COD_IMALU: TIntegerField;
    cds2COD_CONTAS: TIntegerField;
    cds2QUTDE: TIntegerField;
    cds2VALOR: TFloatField;
    cds2VALOR_TOTAL: TFloatField;
    cds2DESCRICAO: TStringField;
    cds2PARCELA: TIntegerField;
    cds2VENCIMENTO: TDateField;
    cds2DATA_LANC: TDateField;
    cds2COD_INQUILINO: TIntegerField;
    cds2COD_MOVIMENTO: TIntegerField;
    cds2DESCRICAO_1: TStringField;
    dspPro: TDataSetProvider;
    cdsPro: TClientDataSet;
    dtsPro: TDataSource;
    sdsPro: TSQLDataSet;
    sdsSP: TSQLDataSet;
    dspSP: TDataSetProvider;
    cdsSP: TClientDataSet;
    dtsSP: TDataSource;
    cdsProNOME_PROPRIETARIO: TStringField;
    cdsProCOD_PROPRIETARIO: TIntegerField;
    cdsProCOD_IMALU: TIntegerField;
    cdsProALUGADO: TStringField;
    cdsProNOME_INQUILINO: TStringField;
    sdsProNOME_PROPRIETARIO: TStringField;
    sdsProCOD_PROPRIETARIO: TIntegerField;
    sdsProCOD_IMALU: TIntegerField;
    sdsProALUGADO: TStringField;
    sdsProNOME_INQUILINO: TStringField;
    sdsProTIPO: TStringField;
    cdsProTIPO: TStringField;
    sdsProRUA: TStringField;
    sdsProNR: TStringField;
    sdsProBAIRRO: TStringField;
    sdsProCIDADE: TStringField;
    sdsProCEP: TStringField;
    sdsProUF: TStringField;
    cdsProRUA: TStringField;
    cdsProNR: TStringField;
    cdsProBAIRRO: TStringField;
    cdsProCIDADE: TStringField;
    cdsProCEP: TStringField;
    cdsProUF: TStringField;
    DBGrid4: TDBGrid;
    XPMenu1: TXPMenu;
    Panel2: TPanel;
    EvDBFind1: TEvDBFind;
    Label1: TLabel;
    dxButton1: TdxButton;
    dxButton2: TdxButton;
    sdsProCOD_INQUILINO: TIntegerField;
    cdsProCOD_INQUILINO: TIntegerField;
    sdsSPCODIGO_PRO: TIntegerField;
    sdsSPCODIGO_INQ: TIntegerField;
    sdsSPALUGUEL: TFloatField;
    sdsSPIPTU: TFloatField;
    sdsSPAGUA: TFloatField;
    sdsSPLUZ: TFloatField;
    sdsSPESGOTO: TFloatField;
    sdsSPDESCONTO: TFloatField;
    sdsSPRECEBIDO: TFloatField;
    sdsSPNOME_PROPRIETARIO: TStringField;
    sdsSPNOME_INQUILINO: TStringField;
    sdsSPVENCIMENTO: TDateField;
    sdsSPTOTAL_ALUGUEL: TFloatField;
    sdsSPTOTAL_RECEBIDO: TFloatField;
    sdsSPTOTAL_DESPESAS: TFloatField;
    sdsSPTOTAL_RECEITAS: TFloatField;
    sdsSPLANC_DESCRICAO: TStringField;
    sdsSPPLAN_DESCRICAO: TStringField;
    sdsSPRECEITA_DESPESA: TStringField;
    sdsSPCODIGO_HT: TIntegerField;
    sdsSPLANC_DESCRICAO1: TStringField;
    sdsSPLANC_DESPESA: TFloatField;
    sdsSPLANC_RECEITA: TFloatField;
    cdsSPCODIGO_PRO: TIntegerField;
    cdsSPCODIGO_INQ: TIntegerField;
    cdsSPALUGUEL: TFloatField;
    cdsSPIPTU: TFloatField;
    cdsSPAGUA: TFloatField;
    cdsSPLUZ: TFloatField;
    cdsSPESGOTO: TFloatField;
    cdsSPDESCONTO: TFloatField;
    cdsSPRECEBIDO: TFloatField;
    cdsSPNOME_PROPRIETARIO: TStringField;
    cdsSPNOME_INQUILINO: TStringField;
    cdsSPVENCIMENTO: TDateField;
    cdsSPTOTAL_ALUGUEL: TFloatField;
    cdsSPTOTAL_RECEBIDO: TFloatField;
    cdsSPTOTAL_DESPESAS: TFloatField;
    cdsSPTOTAL_RECEITAS: TFloatField;
    cdsSPLANC_DESCRICAO: TStringField;
    cdsSPPLAN_DESCRICAO: TStringField;
    cdsSPRECEITA_DESPESA: TStringField;
    cdsSPCODIGO_HT: TIntegerField;
    cdsSPLANC_DESCRICAO1: TStringField;
    cdsSPLANC_DESPESA: TFloatField;
    cdsSPLANC_RECEITA: TFloatField;
    sdsSPTAXA_ADM: TFloatField;
    cdsSPTAXA_ADM: TFloatField;
    DBGrid5: TDBGrid;
    sdsPLANO: TSQLDataSet;
    dspPLANO: TDataSetProvider;
    cdsPLANO: TClientDataSet;
    dtsPLANO: TDataSource;
    sdsPLANOCOD_LANCAMENTO: TIntegerField;
    sdsPLANOCOD_FIXOS: TIntegerField;
    sdsPLANOCOD_IMALU: TIntegerField;
    sdsPLANOCOD_CONTAS: TIntegerField;
    sdsPLANOQUTDE: TIntegerField;
    sdsPLANOVALOR: TFloatField;
    sdsPLANOVALOR_TOTAL: TFloatField;
    sdsPLANODESCRICAO: TStringField;
    sdsPLANOPARCELA: TIntegerField;
    sdsPLANOVENCIMENTO: TDateField;
    sdsPLANODATA_LANC: TDateField;
    sdsPLANOCOD_INQUILINO: TIntegerField;
    sdsPLANOCOD_MOVIMENTO: TIntegerField;
    sdsPLANOCOD_CONTAS_1: TIntegerField;
    sdsPLANODESCRICAO_1: TStringField;
    sdsPLANOGRUPO: TStringField;
    sdsPLANORECEITA_DESPESA: TStringField;
    cdsPLANOCOD_LANCAMENTO: TIntegerField;
    cdsPLANOCOD_FIXOS: TIntegerField;
    cdsPLANOCOD_IMALU: TIntegerField;
    cdsPLANOCOD_CONTAS: TIntegerField;
    cdsPLANOQUTDE: TIntegerField;
    cdsPLANOVALOR: TFloatField;
    cdsPLANOVALOR_TOTAL: TFloatField;
    cdsPLANODESCRICAO: TStringField;
    cdsPLANOPARCELA: TIntegerField;
    cdsPLANOVENCIMENTO: TDateField;
    cdsPLANODATA_LANC: TDateField;
    cdsPLANOCOD_INQUILINO: TIntegerField;
    cdsPLANOCOD_MOVIMENTO: TIntegerField;
    cdsPLANOCOD_CONTAS_1: TIntegerField;
    cdsPLANODESCRICAO_1: TStringField;
    cdsPLANOGRUPO: TStringField;
    cdsPLANORECEITA_DESPESA: TStringField;
    Data1: TDateEdit;
    Label7: TLabel;
    Data2: TDateEdit;
    Label8: TLabel;
    sdsSPPLAN_CODIGO: TIntegerField;
    cdsSPPLAN_CODIGO: TIntegerField;
    Label2: TLabel;
    DBGrid3: TDBGrid;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    DBEdit3: TDBEdit;
    DBEdit4: TDBEdit;
    DBEdit5: TDBEdit;
    DBEdit6: TDBEdit;
    DBEdit7: TDBEdit;
    cdsSPTot_Aluguel: TAggregateField;
    cdsSPTot_IPTU: TAggregateField;
    cdsSPTot_Agua: TAggregateField;
    cdsSPTot_Luz: TAggregateField;
    cdsSPtot_Esgoto: TAggregateField;
    cdsSPtot_desconto: TAggregateField;
    cdsSPtot_outrasDesp: TAggregateField;
    cdsSPtot_outrasRec: TAggregateField;
    DBEdit8: TDBEdit;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    ComboBox1: TComboBox;
    Label14: TLabel;
    Label13: TLabel;
    DBEdit9: TDBEdit;
    cdsSPtotal: TAggregateField;
    VCLReport1: TVCLReport;
    ComboBox2: TComboBox;
    Label15: TLabel;
    procedure BitBtn9Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure dxButton1Click(Sender: TObject);
    procedure dxButton2Click(Sender: TObject);
    procedure DBGrid2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBGrid2DblClick(Sender: TObject);
    procedure BitBtn6Click(Sender: TObject);
    procedure DBGrid2KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure BitBtn8Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fFiltro_Movimento: TfFiltro_Movimento;
  texto, texto1, texto2, DataStr, T_IMPRESSO : string;
implementation

uses uProcurar, UDM;

{$R *.dfm}

procedure TfFiltro_Movimento.BitBtn9Click(Sender: TObject);
begin
  data1.Text:='  /  /    ';
  data2.Text:='  /  /    ';
end;

procedure TfFiltro_Movimento.BitBtn4Click(Sender: TObject);
begin
  T_IMPRESSO := 'TODOS PRO';
  if (cdsSP.Active) then
    cdsSp.Close;

  cdsSp.Params[0].Clear;
  cdsSp.Params[1].Clear;
  cdsSp.Params[2].Clear;
  cdsSp.Params[3].Clear;
  cdsSp.Params[4].Clear;
  cdsSp.Params[0].AsInteger := 9999999;
  cdsSp.Params[1].AsInteger := 9999999;
  //==============================================================================
  datastr:='  /  /    ';
  //==============================================================================
  //------------------------------------------------------------------------------
  //Verifica se a data de vencimento foi preenchido
  //------------------------------------------------------------------------------
  if (data1.Text<>datastr) then
    cdsSp.Params[2].AsDateTime := StrToDateTime(Data1.Text)
  else
  begin
    MessageDlg('preencha o campo Data..', mtWarning, [mbOK], 0);
    exit;
  end;
  if (data2.Text<>datastr) then
    cdsSp.Params[3].AsDateTime := StrToDateTime(Data2.Text)
  else
  begin
    MessageDlg('preencha o campo Data..', mtWarning, [mbOK], 0);
    exit;
  end;
  //------------------------------------------------------------------------------
  //STATUS
  //------------------------------------------------------------------------------
  if ComboBox1.Text<>'' then
    cdsSp.Params[4].AsString := ComboBox1.Text
  else
    cdsSp.Params[4].AsString := 'todos';
      
  cdsSp.Open;
  Label2.Caption := '';  
  Label2.Caption := 'DESPESAS E RECEITAS DE TODOS OS INQUILINOS';
  Label2.Alignment := taCenter;
end;

procedure TfFiltro_Movimento.dxButton1Click(Sender: TObject);
begin
  T_IMPRESSO := 'POR INQUILINO';
  if (cdsSP.Active) then
    cdsSp.Close;

  cdsSp.Params[0].Clear;
  cdsSp.Params[1].Clear;
  cdsSp.Params[2].Clear;
  cdsSp.Params[3].Clear;
  cdsSp.Params[4].Clear;
  cdsSp.Params[0].AsInteger := cdsProCOD_PROPRIETARIO.AsInteger;
  cdsSp.Params[1].AsInteger := cdsProCOD_INQUILINO.AsInteger;
  //==============================================================================
  datastr:='  /  /    ';
  //==============================================================================
  //------------------------------------------------------------------------------
  //Verifica se a data de vencimento foi preenchido
  //------------------------------------------------------------------------------
  if (data1.Text<>datastr) then
    cdsSp.Params[2].AsDateTime := StrToDateTime(Data1.Text)
  else
  begin
    MessageDlg('preencha o campo Data..', mtWarning, [mbOK], 0);
    exit;
  end;
  if (data2.Text<>datastr) then
    cdsSp.Params[3].AsDateTime := StrToDateTime(Data2.Text)
  else
  begin
    MessageDlg('preencha o campo Data..', mtWarning, [mbOK], 0);
    exit;
  end;
  //------------------------------------------------------------------------------
  //STATUS
  //------------------------------------------------------------------------------
  if ComboBox1.Text<>'' then
    cdsSp.Params[4].AsString := ComboBox1.Text
  else
    cdsSp.Params[4].AsString := 'todos';
      
  cdsSp.Open;
  Label2.Caption := '';
  Label2.Caption := 'DESPESAS E RECEITAS DO INQUILINO - ' + cdsSPNOME_INQUILINO.AsString;
  Label2.Alignment := taCenter;  
end;

procedure TfFiltro_Movimento.dxButton2Click(Sender: TObject);
begin

  T_IMPRESSO := 'TODOS INQUILINO';

  if (cdsSP.Active) then
    cdsSp.Close;

  cdsSp.Params[0].Clear;
  cdsSp.Params[1].Clear;
  cdsSp.Params[2].Clear;
  cdsSp.Params[3].Clear;
  cdsSp.Params[4].Clear;
  cdsSp.Params[0].AsInteger := cdsProCOD_PROPRIETARIO.AsInteger;
  cdsSp.Params[1].AsInteger := 9999999;
  //==============================================================================
  datastr:='  /  /    ';
  //==============================================================================
  //------------------------------------------------------------------------------
  //Verifica se a data de vencimento foi preenchido
  //------------------------------------------------------------------------------
  if (data1.Text<>datastr) then
    cdsSp.Params[2].AsDateTime := StrToDateTime(Data1.Text)
  else
  begin
    MessageDlg('preencha o campo Data..', mtWarning, [mbOK], 0);
    exit;
  end;
  if (data2.Text<>datastr) then
    cdsSp.Params[3].AsDateTime := StrToDateTime(Data2.Text)
  else
  begin
    MessageDlg('preencha o campo Data..', mtWarning, [mbOK], 0);
    exit;
  end;
  //------------------------------------------------------------------------------
  //STATUS
  //------------------------------------------------------------------------------
  if ComboBox1.Text<>'' then
    cdsSp.Params[4].AsString := ComboBox1.Text
  else
    cdsSp.Params[4].AsString := 'todos';
    
  cdsSp.Open;
  Label2.Caption := '';
  Label2.Caption := 'DESPESAS E RECEITAS DE TODOS INQUILINO DO PROP. - ' + cdsSPNOME_PROPRIETARIO.AsString;
  Label2.Alignment := taCenter;
end;

procedure TfFiltro_Movimento.DBGrid2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 if cdsSP.Active then
 begin
  if cdsPLANO.Active then
     cdsPLANO.Close;
  cdsPLANO.Params[0].AsInteger := cdsSPPLAN_CODIGO.AsInteger;
  cdsPLANO.Open;
 end;

end;

procedure TfFiltro_Movimento.DBGrid2DblClick(Sender: TObject);
begin
 if cdsSP.Active then
 begin
  if cdsPLANO.Active then
     cdsPLANO.Close;
  cdsPLANO.Params[0].AsInteger := cdsSPCODIGO_HT.AsInteger;
  cdsPLANO.Open;
 end;
end;

procedure TfFiltro_Movimento.BitBtn6Click(Sender: TObject);
begin
  Close;
end;

procedure TfFiltro_Movimento.DBGrid2KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 if cdsSP.Active then
 begin
  if cdsPLANO.Active then
     cdsPLANO.Close;
  cdsPLANO.Params[0].AsInteger := cdsSPCODIGO_HT.AsInteger;
  cdsPLANO.Open;
 end;
end;

procedure TfFiltro_Movimento.FormShow(Sender: TObject);
begin
  if (cdsPro.Active) then
    cdsPro.Close;
  cdsPro.Params[0].Clear;
  cdsPro.Params[1].AsInteger := 9999999;
  cdsPro.Open;
end;

procedure TfFiltro_Movimento.BitBtn8Click(Sender: TObject);
begin
  VCLReport1.Filename := str_relatorio + 'movimento_Inquilino_todos.rep';

  if (T_IMPRESSO = 'POR INQUILINO') then
  begin
    VCLReport1.Report.Params.ParamByName('CODIGO').Value := cdsProCOD_PROPRIETARIO.AsInteger;
    VCLReport1.Report.Params.ParamByName('CODINQ').Value := cdsProCOD_INQUILINO.AsInteger;
  end;

  if (T_IMPRESSO = 'TODOS INQUILINO') then
  begin
    VCLReport1.Report.Params.ParamByName('CODIGO').Value := cdsProCOD_PROPRIETARIO.AsInteger;
    VCLReport1.Report.Params.ParamByName('CODINQ').Value := 9999999;
  end;

  if (T_IMPRESSO = 'TODOS PRO') then
  begin
    VCLReport1.Report.Params.ParamByName('CODIGO').Value := 9999999;
    VCLReport1.Report.Params.ParamByName('CODINQ').Value := 9999999;
  end;

  VCLReport1.Report.Params.ParamByName('DATA1').Value := StrToDateTime(Data1.Text);
  VCLReport1.Report.Params.ParamByName('DATA2').Value := StrToDateTime(Data2.Text);

  if (ComboBox1.Text <> '') then
    VCLReport1.Report.Params.ParamByName('STATUS').Value := ComboBox1.Text
  else
    VCLReport1.Report.Params.ParamByName('STATUS').Value := 'todos';

  VCLReport1.Execute;

end;

end.
